<template>
  <div>
    <van-nav-bar title="联系人" fixed safe-area-inset-top placeholder/>
    <div>
      <div style="margin-top: 5px;">
        <van-cell-group>
          <van-cell is-link @click="toInvitePage">
            <template #title>
              <van-badge dot>
                <div>新的联系人</div>
              </van-badge>
            </template>
          </van-cell>
        </van-cell-group>
      </div>
      <div class="user-wrap">
        <div v-for="(item,index) in list" :key="index" style="border-bottom: 1px solid #dcdde1;" @click="toChatPage(item.uid)">
          <van-swipe-cell>
            <div style="height: 46px; background-color: #ffffff; display: flex; flex-wrap: nowrap; padding: 10px;">
              <div style="width: 46px;">
                <van-image
                  radius="5"
                  width="46"
                  height="46"
                  :src="item.avatar"
                />
              </div>
              <div style="margin-left: 10px; display: flex; align-items: center;">
                <span style="font-size: 18px; color: #303133;">{{ item.uid }}</span>
              </div>
            </div>
            <template #right>
              <van-button square text="删除" type="danger" class="delete-button" @click="deleteUser(item)"/>
            </template>
          </van-swipe-cell>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
import { storeToRefs } from 'pinia'
import { friendListStore } from '@/stores/mqttStore'
import { useRouter } from 'vue-router'

const router = useRouter()
const { list } = storeToRefs(friendListStore())

const toInvitePage = () => {
  router.push({
    path: '/invite'
  })
}

const deleteUser = (user) => {
  list.value = list.value.filter(i => i.uid !== user.uid)
}

const toChatPage = (uid) => {
  router.push({
    path: 'chat',
    query: {uid: uid}
  })
}
</script>

<style lang="less" scoped>
.user-wrap{
  height: calc(100vh - 160px);
  width: 100vw;
  background-color: #f5f6f9;
  overflow: auto;
  padding-top: 5px;
  padding-bottom: 5px;

  .delete-button {
    height: 100%;
  }
}
</style>